package han.gui;


import java.util.ArrayList;

import han.common.RecipeNudel;
import han.project.speakingcookbook.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class CookingView extends Activity implements OnInitListener{
	private ImageButton speakButton;
	private TextToSpeech tts;
	private String speakingTxt;
	private String question;
	private final int TEXT_TO_SPEECH_CODE = 621836183;
	private final int VOICE_RECOGNITION_CODE = 14123123;
	private Intent recognizerIntent;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.cookingview_activity);
		
		speakButton = (ImageButton) findViewById(R.id.start_cooking_button);
		
		speakButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

//				while(count <= RecipeNudel.RECIPE_NUDEL_STEPS) {
					tts.speak("Are you ready?", TextToSpeech.QUEUE_ADD, null);
					startActivityForResult(recognizerIntent, VOICE_RECOGNITION_CODE);
//					try {
//						for (int i = 0; i++ < 60; Thread.sleep(100));
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}

//				}
			}
		});
		Intent initIntent = new Intent();
		initIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(initIntent, TEXT_TO_SPEECH_CODE);
		
		recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
		recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Yes or No?");
		recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
		recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
//		switch(requestCode) {
//		case 1:
//		}
        if (requestCode == TEXT_TO_SPEECH_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                tts = new TextToSpeech(this, this);
            }
            else {
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
        if(requestCode == VOICE_RECOGNITION_CODE) {
        	if (resultCode == RESULT_OK) {
        		ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        		Toast.makeText(CookingView.this, "aufruf", Toast.LENGTH_LONG).show();
        		question = matches.get(0).toString();
				RecipeNudel r = new RecipeNudel();
				int count = 1;
				if(question.equals("yes")) {
					speakingTxt = r.searchPosiInRecipe(count);
					count++;
					if (speakingTxt!=null && speakingTxt.length()>0) {
						tts.speak(speakingTxt, TextToSpeech.QUEUE_ADD, null);
				    }
				} else if (question.equals("no")){
					tts.speak("Iam waiting", TextToSpeech.QUEUE_FLUSH, null);
				} else {
					tts.speak("I can not understand you", TextToSpeech.QUEUE_FLUSH, null);
				}
        	} else {
        		Toast.makeText(CookingView.this, "Some Kind of Error", Toast.LENGTH_LONG).show();
        	}
        }

    }

	@Override
	public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Toast.makeText(CookingView.this, 
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(CookingView.this, 
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
		
	}
	
}
